package com.atguigu.tingshu.user.service.strategy.factory;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.user.service.strategy.PaidRecordStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 管理现有所有策略实现类型对象，对外提供返回策略实现类方法
 *
 * @author: atguigu
 * @create: 2024-12-09 14:21
 */
@Slf4j
@Component
public class PaidRecordStrategyFactory {

    /**
     * @Autowired为Map自动进行注入
     * 将PaidRecordStrategy接口类型下所有实现类全部注入Map中
     * Map中key为实现类Bean对象名称，value为实现类对象
     */
    @Autowired
    private Map<String, PaidRecordStrategy> strategyMap;


    /**
     * 根据商品类型，返回对应策略实现类对象
     * @param type
     * @return
     */
    public PaidRecordStrategy getStrategy(String type) {
        if(strategyMap.containsKey(type)){
            return strategyMap.get(type);
        }
        throw new GuiguException(500, "不支持该种策略！");
    }

}
